package demo.practice.double_pointer;

public class Offer21_2 {

    public static void main(String[] args) {
        Offer21_2 offer21 = new Offer21_2();
        offer21.exchange(new int[]{1,2,3,4});
    }


    public int[] exchange(int[] nums) {
        //int i = 0;
        int j = 0;
        for(int i=0;i<nums.length;i++){
            if( (nums[i]&1)==1 ){//nums[i] 偶数
                swap(nums,i,j);
                j++;
            }
        }
        return nums;
    }

    private void swap(int[] nums, int i, int j) {
        if (i == j) //如果i==j，直接会当前元素更改为0，并且两个位置重合，就会直接变成0，造成错误
            return;
        nums[i] = nums[i] ^ nums[j];
        nums[j] = nums[i] ^ nums[j];
        nums[i] = nums[i] ^ nums[j];
    }

}
